import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

class Student{
    public int studentId;
    public int classId;
}

@WebServlet("/studentInfo")
public class studentInfo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取到QueryString中的值-->根据key获取value
        String queryString = req.getQueryString();
        //getParameter的作用是拿到queryString->根据key来获取value
        //一个请求中queryString是程序员自定义的.key不会没有
        String classId = req.getParameter("classId");
        String studentId = req.getParameter("studentId");
        System.out.println("classId :" + classId + " studentId :" + studentId);
        resp.getWriter().write("classId :" + classId + "studentId :" + studentId);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String queryString = req.getQueryString();
//        String classId = req.getParameter("classId");
//        String studentId = req.getParameter("studentId");
//        System.out.println("classId : " + classId + " studentId : " + classId);
//        resp.getWriter().write("classId : " + classId + " studentId : " + studentId);
        //处理JSON的请求
        ObjectMapper objectMapper = new ObjectMapper();
        //填入流对象,类对象
        //将JSON格式转换为java对象
        Student student = objectMapper.readValue(req.getInputStream(),Student.class);

        System.out.println(student.classId + "," + student.studentId);
        resp.getWriter().write(student.classId + "," + student.studentId);
    }
}
